package ru.mti.edu.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class FilterUsing implements Filter {

	private FilterConfig fc;
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest req = null;
		HttpServletResponse resp = null;
		if (arg0 instanceof HttpServletRequest){
			req = (HttpServletRequest) arg0;
		}
		else return;
		if (arg1 instanceof HttpServletResponse){
			resp = (HttpServletResponse) arg1;
		}
		else return;
		Enumeration<String> enu = req.getParameterNames();
		while(enu.hasMoreElements()){
			String paramName = enu.nextElement();
			System.out.println("Parameter '" + paramName + "' = " + req.getParameter(paramName));
		}
		HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req){

			@Override
			public String getParameter(String name) {
				String value = super.getParameter(name);
				System.out.println("Parameter '" + name + "' = " + value);
				return value;
			}
			
		};
		
		arg2.doFilter(reqWrapper, resp);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		this.fc = arg0;
	}

}
